<?php
namespace app\index\controller;
use Payment\Notify\PayNotifyInterface;
use Payment\Config;
use think\Request;
use think\Db;
/**
 * @author: helei
 * @createTime: 2016-07-20 18:31
 * @description:
 */

/**
 * 客户端需要继承该接口，并实现这个方法，在其中实现对应的业务逻辑
 * Class TestNotify
 * anthor helei
 */
class NotifyUrl implements PayNotifyInterface
{
    public function memberAccountChange($out_trade_no){
        $order=Db::name('noordernum')->where('NoOrderNumId',$out_trade_no)->find();
        $account=Db::name('member_account')->where('memberId',$order['memberId'])->value('account');
        $paltAmount = Db::name('member_account_log')->where('memberId', 0)->sum('changeAccount') + 0;
        Db::name('member_account_log')->insert(['memberAccountLogId' => autoId(), 'memberId' =>$order['memberId'], 'balanceAccount' =>$account+$order['amount'], 'changeAccount' => $order['amount'], 'businessId' =>$out_trade_no, 'businessType' => 10, 'isAuto' =>1,  'addDateTime' => date("Y-m-d H:i:s") , 'operator' => $order['memberId']],'tradeMemberId',$order['memberId']);
        Db::name('member_account_log')->insert(['memberAccountLogId' => autoId(), 'memberId' =>0, 'balanceAccount' =>$paltAmount+$order['amount'], 'changeAccount' => $order['amount'], 'businessId' =>$out_trade_no, 'businessType' => 10, 'isAuto' =>1,  'addDateTime' => date("Y-m-d H:i:s") , 'operator' => $order['memberId']],'tradeMemberId',$order['memberId']);
        Db::name('member_account')->where('memberId',$order['memberId'])->update(['account'=>$account+$order['amount'],'changeTime'=>date("Y-m-d H:i:s")]);
    }
    public function notifyProcess(array $data)
    {
        $channel = $data['channel'];
        if ($channel === Config::ALI_CHARGE) {// 支付宝支付
        } elseif ($channel === Config::WX_CHARGE) {// 微信支付
            $out_trade_no =   $data['order_no'];
            //查询订单号否正确
            $type =  substr($out_trade_no,0,2);
            //支付宝返回的信息
            $postInfo = array(
                'out_trade_no' => $out_trade_no,
                'total_amount' => $data['amount'],
            );
            if($type == 'JM'||$type == 'CZ'){
                $order =  Db::name('noordernum')->where('NoOrderNumId',$out_trade_no)->find();
//                //查询数据是否已经更新
//                if($order['payStatus'] !=0){
//                    return  true;
//                }
                if($order['amount'] == $data['amount']){

                    if($data['trade_state']== 'SUCCESS'){
                        //支付成功
                        //订单解密
                        Db::name('noordernum')->where('NoOrderNumId',$out_trade_no)->update(array('payStatus'=>1, "DateTime" =>aTime(),'payType'=>'微信'));
                        if($type == 'JM'){
                            //返现
                            $order = new \app\index\controller\Order();
                            $order->registerAccountChange($out_trade_no);
                        }
                        if($type == 'CZ'){
                            // 操作数据
                            $order = new \app\index\controller\Order();
                            $order ->memberAccountChange($out_trade_no,"微信");
                        }
                    }else{
                        //支付失败
                        Db::name('noordernum')->where('NoOrderNumId',$out_trade_no)->update(array('payStatus'=>2, "DateTime" =>aTime(),'payType'=>'微信'));
                    }
                }
            }else{
                //服务订单（订单成功记得返回success给支付宝）
                $soId = Db::name('service_order so')->where('so.serviceOrderId',$out_trade_no)->find();
//                if($soId['paymentStatus'] != 0){
//                    return true;
//                }
                if($soId['orderAmount'] == $data['amount']){

                    if($data['trade_state']== 'SUCCESS'){
                        //支付成功
                            $orderPay = new \app\index\controller\Order();
                            $orderPay->serviceOrderPay($out_trade_no);
                    }else{
                        //支付失败
                        Db::name('noordernum')->where('NoOrderNumId',$out_trade_no)->update(array('payStatus'=>2, "DateTime" =>aTime(),'payType'=>'微信'));
                    }
                }
            }
        } elseif ($channel === Config::CMB_CHARGE) {// 招商支付

        } elseif ($channel === Config::CMB_BIND) {// 招商签约

        } else {
            // 其它类型的通知
        }

        // 执行业务逻辑，成功后返回true
        return true;
    }
}
